/*
 * @Description: 一些以前定义的方法,生成打印预览的意见区
 */
import globalData from '@/plugins/data';
function stringEncode(str) {
  var div = document.createElement('div');
  if (div.innerText) {
    div.innerText = str;
  } else {
    div.textContent = str;
  }
  return div.innerHTML;
}
// xss转换
function xssReplaceStr(tblStr) {
  tblStr = tblStr.replace(/<script>/gi, stringEncode('<script>'));
  // 替换字符串变量或者结束标签这样写
  tblStr = tblStr.replace(new RegExp('</script>', 'gi'), stringEncode('</script>'));
  return tblStr;
}
/**
 * @description: 生成附件和关联文档的带样式html字符串
 * @param {Array}list 附件列表或者关联文档列表
 * @param {String}title 值为附件、或者关联文档
 * @return {String}打印用的html字符串
 */
export function createDocumentList(list, title) {
  //生成列表
  const listHTML = list
    .map(
      (item) => `<li style="
                                border:1px solid #e6eef7;
                                padding: 5px 10px;
                                display: flex;
                                margin: 0 5px 5px;
                                align-items: center;
                                border-radius: 2px;
                            ">
                                <img
                                    style="width: 16px;"
                                    src="${globalData.ctxPath}/common/images/attachmentICON/${
        item.icon
      }"
                                />
                                <div style="margin-left: 5px">${item.filename.escapeHTML()}</div>
                        </li>`
    )
    .join('');

  //生成整个模块内容
  return cleanSpecial(`<div style="display:flex; margin-top: 20px">
                            <div class='div-float' style='
                            color: #335186;
                            padding-top:8px;
                            font-weight: bolder;
                            font-size: 12px;
                            margin-right:10px;'>
                                ${title}: (${list.length})
                            </div>
                            <ul style="display: flex; flex-wrap: wrap;flex:1;cursor:default;">
                            ${listHTML}
                            </ul>
                        </div>`);
}

/**
 * @description 生成附件和关联文档区
 * @param {Array} list 相关文件列表，包含附件或者关联文档或者都有,  attachments
 * @param {String} title 值为附件、或者关联文档
 * @return {String} 打印用的html字符串
 */
export function createOptionDoumentList(list, title) {
  if (!list) {
    return '';
  }
  const optionDoucmentListHtml = list
    .map((item) =>
      title === 'attachments'
        ? `<div style="padding:5px;margin: 0 10px 10px 0;background-color: rgb(230, 238, 247);border-radius: 3px;font-size:14px;">
                        <span class="ico16 ${item.extension}_16 margin_r_5"></span>
                            <span>${item.filename}</span>
                    </div>
            `
        : `<div style="padding:5px;margin: 0 10px 10px 0;background-color: rgb(230, 238, 247);border-radius: 3px;font-size:14px;">
                        <span class="syDoc ico16 collaboration_16"></span>
                            <span>${item.filename}</span>
                    </div>
            `
    )
    .join('');
  let attachmentTypeStr = '<em class="show-attachment syIcon sy-attachment"></em>';
  let associatedTypeStr = '<em class="syIcon sy-associated_document"></em>'; // 附件和关联文档图标
  return cleanSpecial(`<div style="display: flex;margin-top: 10px;font-weight:500;font-size:14px;color:#111;">
                                        <div style="width: 6%;">
                                            ${
                                              title === 'attachments'
                                                ? attachmentTypeStr
                                                : title === 'relationDocs'
                                                ? associatedTypeStr
                                                : ''
                                            }
                                            <span>(${list.length || 0})</span>
                                        </div>
                                        <div style="width: 94%;margin-left: 10px;display: flex;flex-wrap: wrap;">
                                            ${optionDoucmentListHtml}
                                        </div>
                                    </div>
        `);
}

/**
 * @description 生成处理人意见区的回复
 * @param {Object} payload 一条意见回复的相关数据
 * @return {String}打印用的html字符串
 */
export function createOptionReplyList(payload = {}) {
  const borderTopStr = 'border-top: none;'; // 避免完整的边框重合
  const OptionReplyListHtml = payload.children.length
    ? payload.children
        .map(
          (item, index) => `<div style="padding: 10px;border:1px solid #e3e3e1;${
            index !== 0 ? borderTopStr : ''
          }">
                        <div class="reply-info" style="display: flex;justify-content: space-between;">
                            <div style="font-size: 14px;">${item.createName}</div>
                            <div style="font-size: 12px;color: #a3a3a3;">${getSendTime(
                              item.createDate
                            )}</div>
                        </div>
                        <div class="reply-content" style="margin-top: 10px;">${$.i18n(
                          'cap4.bind.label.reply'
                        )} ${item.replyToName}：${item.escapedContent || item.content}</div>
                        <div style="font-size: 12px; color: #111;">
                            ${
                              item.attFileDomain.length
                                ? createOptionDoumentList(item.attFileDomain, 'attachments')
                                : ''
                            }
                            ${
                              item.assDocDomain.length
                                ? createOptionDoumentList(item.assDocDomain, 'relationDocs')
                                : ''
                            }
                        </div>
                    </div>
            
            `
        )
        .join('')
    : '';
  let praiseStr = 'sy-praise-fill like_16';
  let noPraiseStr = 'sy-praise no_like_disable_16';
  return cleanSpecial(`<div class="processing-view">
                                <div class="content" style="width: 750px;margin: 20px auto;">
                                    <div class="reply-data">
                                        <div class="per-title" style="display: flex;justify-content: space-between;background-color: #f0f0ee;padding: 10px 20px">
                                            <div class="left-info" style="display: flex;align-items: center;">
                                                <img src="${
                                                  payload.avatarImageUrl
                                                }" alt="" style="width: 32px;height: 32px;border-radius: 50%;margin-right: 10px;">
                                                <span style="margin-right: 10px;font-size: 14px;">${
                                                  payload.createName
                                                }</span>
                                                <span style="margin-right: ${
                                                  payload.extAtt1I18n ? '10px' : '0'
                                                };font-weight: bold;font-size: 14px;">${
    payload.extAtt1I18n || ''
  }</span>
                                                <span style="margin-right: 10px;font-weight: bold;font-size: 14px;">${
                                                  (payload.extAtt3I18n &&
                                                  payload.extAtt3 !==
                                                    'collaboration.dealAttitude.repeat.auto.skip'
                                                    ? payload.extAtt3I18n
                                                    : '') || ''
                                                }</span>
                                                <span style="margin-right: 10px;font-size: 14px;color: #ada3a0;">${getSendTime(
                                                  payload.createDate
                                                )}</span>
                                            </div>
                                            <div class="right-info" style="display: flex;align-items: center;">
                                                <span class="syIcon  display_inline-block ${
                                                  payload.praiseNumber === 0
                                                    ? noPraiseStr
                                                    : praiseStr
                                                } "></span> 
                                                <span style="color: #ada3a0;margin-left:10px">${
                                                  payload.praiseNumber
                                                }</span>
                                            </div>
                                        </div>
                                        <div class="option-content" style="width: 700px;margin: 0 auto;font-size: 14px;">
                                            <div style="margin: 10px 0;">${xssReplaceStr(
                                              payload.escapedContent || payload.content || ''
                                            )}</div>
                                            <div style="font-size: 12px; color: #111;">
                                                ${
                                                  payload.attFileDomain.length
                                                    ? createOptionDoumentList(
                                                        payload.attFileDomain,
                                                        'attachments'
                                                      )
                                                    : ''
                                                }
                                                ${
                                                  payload.assDocDomain.length
                                                    ? createOptionDoumentList(
                                                        payload.assDocDomain,
                                                        'relationDocs'
                                                      )
                                                    : ''
                                                }
                                            </div>
                                            <div class="reply-container" style="background-color: #fafaf7;">
                                                ${OptionReplyListHtml}
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            `);
}

/**
 * @description 生成处理人意见区
 * @param {Array} list 回复内容列表
 * @return {String}打印用的html字符串
 */
export function createOptionList(payload,forward=false) {
  const optionListHtml = payload.comments
    .map((item) => {
      if (item.ctype == 0)
        // 转发的 判定是 意见区
        return `${createOptionReplyList(item)}`;
    })
    .join('');
  const postscriptHtml = payload.comments
    .map((item) => {
      if (item.ctype == -1) {
        // 转发的 判定是 附言区
        return `${createPostscriptList([item])}`;
      } else {
        return [];
      }
    })
    .join('');
  return cleanSpecial(`   ${postscriptHtml.length ? postscriptHtml : ''}
                            <div class="append-html"
                                style="padding-bottom: 5px; width: 800px; margin:0 auto;">
                                <div class="li-title" style="padding: 10px 0;font-size:14px;font-weight: bold;color: #666;cursor: pointer;">
                                    <div>${$.i18n('collaboration.opinion.handleOpinion')} ${$.i18n(
    'collaboration.opinion.handleOpinion.total1',
    payload.commentTotal||0,
    payload.praiseTotal||0
  )} 
    ${ forward? '':  $.i18n('collaboration.opinion.relatedWithMe')}${ forward? '':  $.i18n(
    'cap.template.query.totalNum',
    payload.relatedToMe||0
  )}</div>
                                </div>
                                ${optionListHtml}
                            </div> `);
}

/**
 * @description 生成发起人附言区域
 * @param {Array} list 发起人附言内容列表   extAtt2,createName不确定具体用那个
 * @returns {String}打印用的html字符串
 */
export function createPostscriptList(list) {
  const postscriptListHtml = list
    .map(
      (item) => `<div>
                            <div class="per-content" style="font-size: 14px;margin: 12px 0;">
                                ${xssReplaceStr(item.content)}
                            </div>
                            ${
                              item.attFileDomain.length
                                ? createOptionDoumentList(item.attFileDomain, 'attachments')
                                : ''
                            }
                            ${
                              item.assDocDomain.length
                                ? createOptionDoumentList(item.assDocDomain, 'relationDocs')
                                : ''
                            }
                            <div class="per-date" style="font-size: 12px;color: #a3a3a3;">${getSendTime(
                              item.createDate
                            )}</div>
                        </div>
        `
    )
    .join('');
  return cleanSpecial(`<div class="append-html" style="padding-bottom: 5px; width: 800px; margin:0 auto;">
                            <div class="li-title" style="padding: 10px 0;font-size:14px;font-weight: bold;color: #666;cursor: pointer;">
                                <div>${$.i18n('collaboration.sender.postscript')}</div>
                            </div>
                            <div class="processing-view">
                                <div class="postscript-data" style="width: 750px;margin:0 auto;">
                                    ${postscriptListHtml}
                                </div>
                            </div>
                        </div>
    `);
}
/**
 * @description 生成转发意见
 * @param {Array} list 转发意见的内容列表
 * @returns {String}打印用的html字符串
 */
export function createForwardList(list) {
  const forwardListHtml = list
    .map(
      (
        item
      ) => `<div class="append-html-forward" style="padding-bottom: 5px; width: 800px; margin:0 auto;">
                                <div class="li-title" style="padding: 10px 0;font-size:14px;font-weight: bold;color: #666;cursor: pointer;">
                                    <div>${$.i18n(
                                      'collaboration.forward.oriOp.level.label',
                                      item.forwardNum
                                    )}</div>
                                </div>
                                <div class="forward-container" style="padding: 0 15px;">
                                    ${createOptionList(item,true)}
                                </div>
                            </div>  
                        `
    )
    .join('');
  return cleanSpecial(`<div>${forwardListHtml}</div>`);
}

/**
 * @description 时间戳转时间
 * @param {String} time 时间戳
 * @returns {String} 时间日期
 */
function getSendTime(time) {
  if (time && (time + '').indexOf(':') > 0) {
    return time;
  }
  time = +time;
  var date = new Date(time + 8 * 3600 * 1000); // 增加8小时，转换到东八区
  return date.toJSON().substr(0, 19).replace('T', ' ');
}
